package com.cincc.cincc_pbx.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cincc.cincc_pbx.R;
import com.cincc.cincc_pbx.model.ExternalContactModel;

import java.util.List;

public class ExternalAdapter extends RecyclerView.Adapter<ExternalAdapter.MyViewHolder> {

    private Context context;
    private List<ExternalContactModel> list;

    public ExternalAdapter(Context context, List<ExternalContactModel> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.external_contact_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        ExternalContactModel item = list.get(position);
        holder.nameText.setText(item.getName());
        holder.itemView.setOnClickListener(v -> {
            iOnClickItem.OnClick(item);
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface IOnClickItem {
        void OnClick(ExternalContactModel item);
    }

    private IOnClickItem iOnClickItem;

    public void setOnClickItem(IOnClickItem iOnClickItem) {
        this.iOnClickItem = iOnClickItem;
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView headImg;
        TextView nameText;
        ImageView rightImg;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            headImg = itemView.findViewById(R.id.head_img);
            nameText = itemView.findViewById(R.id.name_tv);
            rightImg = itemView.findViewById(R.id.right_arrow_img);
        }
    }
}
